using MicroMsg.Common.Utils;
using MicroMsg.Network;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml.Linq;
namespace MicroMsg.Manager
{
	public class AppMsgMgr
	{
		public const string TAG = "AppMsgMgr";
		public static AppMsgInfo parseAppXml(string strXml)
		{
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			if (!strXml.StartsWith("<"))
			{
				Log.smethod_5("AppMsgMgr", "strXml need cut");
				int num = strXml.IndexOf("<", 0);
				strXml = strXml.Substring(num);
			}
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			AppMsgInfo result;
			try
			{
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("msg");
				if (xElement == null)
				{
					result = null;
				}
				else
				{
					AppMsgInfo appMsgInfo = new AppMsgInfo();
					XElement xElement2 = xElement.Element("fromusername");
					if (xElement2 != null)
					{
						appMsgInfo.fromUserName = xElement2.get_Value();
					}
					XElement xElement3 = xElement.Element("appmsg");
					XAttribute xAttribute = xElement3.Attribute("appid");
					if (xAttribute != null)
					{
						appMsgInfo.appid = xAttribute.get_Value();
					}
					xAttribute = xElement3.Attribute("sdkver");
					if (xAttribute != null)
					{
						appMsgInfo.sdkVer = xAttribute.get_Value();
					}
					XElement xElement4 = xElement3;
					if (xElement4 != null)
					{
						XElement xElement5 = xElement4.Element("title");
						if (xElement5 != null)
						{
							appMsgInfo.title = xElement5.get_Value();
						}
						XElement xElement6 = xElement4.Element("des");
						if (xElement6 != null)
						{
							appMsgInfo.description = xElement6.get_Value();
						}
						XElement xElement7 = xElement4.Element("action");
						if (xElement7 != null)
						{
							appMsgInfo.action = xElement7.get_Value();
						}
						XElement xElement8 = xElement4.Element("type");
						if (xElement8 != null)
						{
							appMsgInfo.type = int.Parse(xElement8.get_Value());
						}
						XElement xElement9 = xElement4.Element("showtype");
						if (xElement9 != null)
						{
							appMsgInfo.showtype = int.Parse(xElement9.get_Value());
						}
						XElement xElement10 = xElement4.Element("content");
						if (xElement10 != null)
						{
							appMsgInfo.content = xElement10.get_Value();
						}
						XElement xElement11 = xElement4.Element("url");
						if (xElement11 != null)
						{
							appMsgInfo.string_0 = xElement11.get_Value();
						}
						XElement xElement12 = xElement4.Element("lowurl");
						if (xElement12 != null)
						{
							appMsgInfo.lowurl = xElement12.get_Value();
						}
						XElement xElement13 = xElement4.Element("appattach");
						if (xElement13 != null)
						{
							XElement xElement14 = xElement13.Element("totallen");
							if (xElement14 != null)
							{
								appMsgInfo.totallength = int.Parse(xElement14.get_Value());
							}
							XElement xElement15 = xElement13.Element("attachid");
							if (xElement15 != null)
							{
								appMsgInfo.attachid = xElement15.get_Value();
							}
							XElement xElement16 = xElement13.Element("fileext");
							if (xElement16 != null)
							{
								appMsgInfo.fileext = xElement16.get_Value();
							}
						}
						XElement xElement17 = xElement4.Element("extinfo");
						if (xElement17 != null)
						{
							appMsgInfo.extinfo = xElement17.get_Value();
						}
						XElement xElement18 = xElement4.Element("mmreader");
						MMReaderInfo mMReaderInfo = null;
						if (xElement18 != null)
						{
							xElement18 = xElement18.Element("category");
							if (xElement18 != null)
							{
								mMReaderInfo = new MMReaderInfo();
								XAttribute xAttribute2 = xElement18.Attribute("type");
								if (xAttribute2 != null)
								{
									mMReaderInfo.type = int.Parse(xAttribute2.get_Value());
								}
								xAttribute2 = xElement18.Attribute("count");
								if (xAttribute2 != null)
								{
									mMReaderInfo.count = int.Parse(xAttribute2.get_Value());
								}
								XElement xElement19 = xElement18.Element("name");
								if (xElement19 != null)
								{
									mMReaderInfo.name = xElement19.get_Value();
								}
								XElement xElement20 = xElement18.Element("topnew");
								if (xElement20 != null)
								{
									MMReaderTopNew mMReaderTopNew = new MMReaderTopNew();
									XElement xElement21 = xElement20.Element("cover");
									if (xElement21 != null)
									{
										mMReaderTopNew.cover = xElement21.get_Value();
									}
									xElement21 = xElement20.Element("width");
									if (xElement21 != null)
									{
										mMReaderTopNew.width = int.Parse(xElement21.get_Value());
									}
									xElement21 = xElement20.Element("height");
									if (xElement21 != null)
									{
										mMReaderTopNew.height = int.Parse(xElement21.get_Value());
									}
									mMReaderInfo.topnew = mMReaderTopNew;
								}
								IEnumerable<XElement> enumerable = xElement18.Elements("item");
								if (enumerable != null)
								{
									List<MMReaderItem> list = new List<MMReaderItem>();
									using (IEnumerator<XElement> enumerator = enumerable.GetEnumerator())
									{
										while (enumerator.MoveNext())
										{
											XElement current = enumerator.get_Current();
											MMReaderItem mMReaderItem = new MMReaderItem();
											if (current != null)
											{
												XElement xElement22 = current.Element("title");
												if (xElement22 != null)
												{
													mMReaderItem.title = xElement22.get_Value();
												}
												XElement xElement23 = current.Element("digest");
												if (xElement23 != null)
												{
													mMReaderItem.digest = xElement23.get_Value();
												}
												XElement xElement24 = current.Element("url");
												if (xElement24 != null)
												{
													mMReaderItem.string_0 = xElement24.get_Value();
												}
												XElement xElement25 = current.Element("shorturl");
												if (xElement25 != null)
												{
													mMReaderItem.shorturl = xElement25.get_Value();
												}
												XElement xElement26 = current.Element("longurl");
												if (xElement26 != null)
												{
													mMReaderItem.longurl = xElement26.get_Value();
												}
												XElement xElement27 = current.Element("pub_time");
												if (xElement27 != null)
												{
													mMReaderItem.pub_time = long.Parse(xElement27.get_Value());
												}
												XElement xElement28 = current.Element("cover");
												if (xElement28 != null)
												{
													mMReaderItem.cover = xElement28.get_Value();
												}
												XElement xElement29 = current.Element("tweetid");
												if (xElement29 != null)
												{
													mMReaderItem.tweetid = xElement29.get_Value();
												}
												XElement xElement30 = current.Element("sources");
												if (xElement30 != null)
												{
													IEnumerable<XElement> enumerable2 = xElement30.Elements("source");
													if (enumerable2 != null)
													{
														List<MMReaderItemSource> list2 = new List<MMReaderItemSource>();
														using (IEnumerator<XElement> enumerator2 = enumerable.GetEnumerator())
														{
															while (enumerator2.MoveNext())
															{
																XElement current2 = enumerator2.get_Current();
																MMReaderItemSource mMReaderItemSource = new MMReaderItemSource();
																XElement xElement31 = current2.Element("name");
																if (xElement31 != null)
																{
																	mMReaderItemSource.name = xElement31.get_Value();
																}
																XElement xElement32 = current2.Element("icon");
																if (xElement32 != null)
																{
																	mMReaderItemSource.icon = xElement32.get_Value();
																}
																list2.Add(mMReaderItemSource);
															}
														}
														if (list2.get_Count() != 0)
														{
															mMReaderItem.sources = list2;
														}
													}
												}
												list.Add(mMReaderItem);
											}
										}
									}
									if (list.get_Count() != 0)
									{
										mMReaderInfo.items = list;
									}
								}
							}
						}
						appMsgInfo.mmreader = mMReaderInfo;
					}
					result = appMsgInfo;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("AppMsgMgr", ex.ToString() + strXml);
				result = null;
			}
			return result;
		}
		public static AppMusicInfo getMusicInfo(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return null;
			}
			AppMsgInfo appMsgInfo = AppMsgMgr.parseAppXml(chatMsg_0.strMsg);
			if (appMsgInfo == null)
			{
				return null;
			}
			if (appMsgInfo.type != 3)
			{
				return null;
			}
			if (string.IsNullOrEmpty(appMsgInfo.string_0) && string.IsNullOrEmpty(appMsgInfo.lowurl))
			{
				Log.smethod_1("AppMsgMgr", "err lowurl = " + appMsgInfo.lowurl + " url" + appMsgInfo.string_0);
				return null;
			}
			string text = string.IsNullOrEmpty(appMsgInfo.lowurl) ? appMsgInfo.string_0 : appMsgInfo.lowurl;
			if (NetworkDeviceWatcher.isCurrentWifi() && !string.IsNullOrEmpty(appMsgInfo.string_0))
			{
				text = appMsgInfo.string_0;
			}
			int num = text.IndexOf("wechat_music_url=");
			if (num < 0)
			{
				num = text.IndexOf("p=");
				if (num < 0)
				{
					Log.smethod_1("AppMsgMgr", "invalid sourceurl = " + text);
					return null;
				}
				num += "p=".get_Length();
			}
			else
			{
				num += "wechat_music_url=".get_Length();
			}
			string text2 = text.Substring(num);
			if (string.IsNullOrEmpty(text2))
			{
				Log.smethod_1("AppMsgMgr", "invalid bcdstring = " + text2);
				return null;
			}
			string strJson = Util.BcdToStr(text2);
			AppMusicInfo appMusicInfo = (AppMusicInfo)Util.GetObjectFromJson(strJson, typeof(AppMusicInfo));
			if (appMusicInfo == null)
			{
				return null;
			}
			return appMusicInfo;
		}
		public static MMReaderInfo parseMMReaderXml(string strXml)
		{
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			if (!strXml.StartsWith("<"))
			{
				Log.smethod_5("AppMsgMgr", "strXml need cut");
				int num = strXml.IndexOf("<", 0);
				strXml = strXml.Substring(num);
			}
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			MMReaderInfo result;
			try
			{
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("mmreader");
				MMReaderInfo mMReaderInfo = null;
				if (xElement != null)
				{
					xElement = xElement.Element("category");
					if (xElement != null)
					{
						mMReaderInfo = new MMReaderInfo();
						XAttribute xAttribute = xElement.Attribute("type");
						if (xAttribute != null)
						{
							mMReaderInfo.type = int.Parse(xAttribute.get_Value());
						}
						xAttribute = xElement.Attribute("count");
						if (xAttribute != null)
						{
							mMReaderInfo.count = int.Parse(xAttribute.get_Value());
						}
						XElement xElement2 = xElement.Element("name");
						if (xElement2 != null)
						{
							mMReaderInfo.name = xElement2.get_Value();
						}
						XElement xElement3 = xElement.Element("topnew");
						if (xElement3 != null)
						{
							MMReaderTopNew mMReaderTopNew = new MMReaderTopNew();
							XElement xElement4 = xElement3.Element("cover");
							if (xElement4 != null)
							{
								mMReaderTopNew.cover = xElement4.get_Value();
								int num2 = mMReaderTopNew.cover.IndexOf("|", 0);
								if (num2 > 0)
								{
									mMReaderTopNew.cover = mMReaderTopNew.cover.Substring(0, num2);
								}
							}
							xElement4 = xElement3.Element("width");
							if (xElement4 != null)
							{
								mMReaderTopNew.width = int.Parse(xElement4.get_Value());
							}
							xElement4 = xElement3.Element("height");
							if (xElement4 != null)
							{
								mMReaderTopNew.height = int.Parse(xElement4.get_Value());
							}
							mMReaderInfo.topnew = mMReaderTopNew;
						}
						IEnumerable<XElement> enumerable = xElement.Elements("newitem");
						ObservableCollection<XElement> observableCollection = null;
						if (enumerable != null)
						{
							observableCollection = enumerable.ToObservableCollection<XElement>();
							if (observableCollection.get_Count() <= 0)
							{
								enumerable = xElement.Elements("item");
								if (enumerable != null)
								{
									observableCollection = enumerable.ToObservableCollection<XElement>();
								}
							}
						}
						if (observableCollection != null)
						{
							List<MMReaderItem> list = new List<MMReaderItem>();
							using (IEnumerator<XElement> enumerator = observableCollection.GetEnumerator())
							{
								while (enumerator.MoveNext())
								{
									XElement current = enumerator.get_Current();
									MMReaderItem mMReaderItem = new MMReaderItem();
									if (current != null)
									{
										XElement xElement5 = current.Element("title");
										if (xElement5 != null)
										{
											mMReaderItem.title = xElement5.get_Value();
										}
										XElement xElement6 = current.Element("digest");
										if (xElement6 != null)
										{
											mMReaderItem.digest = xElement6.get_Value();
										}
										XElement xElement7 = current.Element("url");
										if (xElement7 != null)
										{
											mMReaderItem.string_0 = xElement7.get_Value();
										}
										XElement xElement8 = current.Element("shorturl");
										if (xElement8 != null)
										{
											mMReaderItem.shorturl = xElement8.get_Value();
										}
										XElement xElement9 = current.Element("longurl");
										if (xElement9 != null)
										{
											mMReaderItem.longurl = xElement9.get_Value();
										}
										XElement xElement10 = current.Element("pub_time");
										if (xElement10 != null)
										{
											mMReaderItem.pub_time = long.Parse(xElement10.get_Value());
										}
										XElement xElement11 = current.Element("cover");
										if (xElement11 != null)
										{
											mMReaderItem.cover = xElement11.get_Value();
											int num3 = mMReaderItem.cover.IndexOf("|", 0);
											if (num3 > 0)
											{
												mMReaderItem.cover = mMReaderItem.cover.Substring(0, num3);
											}
										}
										XElement xElement12 = current.Element("tweetid");
										if (xElement12 != null)
										{
											mMReaderItem.tweetid = xElement12.get_Value();
										}
										XElement xElement13 = current.Element("sources");
										if (xElement13 != null)
										{
											IEnumerable<XElement> enumerable2 = xElement13.Elements("source");
											if (enumerable2 != null)
											{
												List<MMReaderItemSource> list2 = new List<MMReaderItemSource>();
												using (IEnumerator<XElement> enumerator2 = enumerable.GetEnumerator())
												{
													while (enumerator2.MoveNext())
													{
														XElement current2 = enumerator2.get_Current();
														MMReaderItemSource mMReaderItemSource = new MMReaderItemSource();
														XElement xElement14 = current2.Element("name");
														if (xElement14 != null)
														{
															mMReaderItemSource.name = xElement14.get_Value();
														}
														XElement xElement15 = current2.Element("icon");
														if (xElement15 != null)
														{
															mMReaderItemSource.icon = xElement15.get_Value();
														}
														list2.Add(mMReaderItemSource);
													}
												}
												if (list2.get_Count() != 0)
												{
													mMReaderItem.sources = list2;
												}
											}
										}
										list.Add(mMReaderItem);
									}
								}
							}
							if (list.get_Count() != 0)
							{
								mMReaderInfo.items = list;
							}
						}
					}
				}
				result = mMReaderInfo;
			}
			catch (Exception ex)
			{
				Log.smethod_1("AppMsgMgr", ex.get_Message());
				result = null;
			}
			return result;
		}
		public static bool isFileSupport(string fileName)
		{
			return AppMsgMgr.isTxtFile(fileName) || AppMsgMgr.isPictureFile(fileName);
		}
		public static bool isTxtFile(string fileName)
		{
			if (string.IsNullOrEmpty(fileName))
			{
				return false;
			}
			fileName = fileName.Trim().ToLower();
			return fileName.EndsWith(".txt") || fileName.EndsWith(".log") || fileName.EndsWith(".xml") || fileName.EndsWith(".xsl");
		}
		public static bool isPictureFile(string fileName)
		{
			if (string.IsNullOrEmpty(fileName))
			{
				return false;
			}
			fileName = fileName.Trim().ToLower();
			return fileName.EndsWith(".png") || fileName.EndsWith(".jpg");
		}
	}
}
